//
// Created by tongj on 2023/10/6.
//

#include <MessageQueue/Message.h>
#include <base/log.hpp>
#include <chrono>
#include <base/buffer.h>

namespace MessageQueue {
using namespace tbs;

IConsumable::StatusEnum IConsumable::status() CONST {
  return this->_status;
}

IConsumable::StatusEnum IConsumable::consume() {
  StatusEnum res = this->_consume();
  setStatus(res);
  return res;
}

void IConsumable::setStatus(CONST IConsumable::StatusEnum &statusEnum) {
  this->_status = statusEnum;
  auto ls = _listener();
  if (ls != nullptr) {
	ls(*this);
  }
}

IConsumable::consume_listenner IConsumable::_listener() {
  return nullptr;
}

IConsumable::StatusEnum DelayMessage::_consume() {
  if (_method == nullptr) {
	return Drop;
  }
  try {
	return this->_method(*this);
  }
  catch (std::runtime_error &e) {
	return Retry;
  }
}

u_size DelayMessage::_index() CONST {
  return this->_time;
}

DelayMessage::DelayMessage(DelayMessage::_consumeMethod method)
	: Buffer(32) {
  this->_method = method;
}

void DelayMessage::setTime(long long int t) {
  this->_time = t;
}

long long DelayMessage::getTime() CONST {
  return this->_time;
}

MsgQueue *DelayMessage::owner()const {
  return _owner;
}

IConsumable::consume_listenner DelayMessage::_listener() {
  return _m_listener;
}

DelayMessage &DelayMessage::setConsumeListener(IConsumable::consume_listenner consumeListenner) {
  this->_m_listener = consumeListenner;
  return *this;
}

DelayMessage &DelayMessage::setConsumeMethod(DelayMessage::_consumeMethod method) {
  this->_method = method;
  return *this;
}
}
